#include "viewer.h"





//-----------------------------------------------------------------------//
// app_draw_refresh_cb
// - Refresh images in principal.
//-----------------------------------------------------------------------//
int app_draw_refresh_cb(Ihandle * self) 
{
	// Update image in canvas only a time for one loop in idle
	IupGLSwapBuffers(self);
	return IUP_DEFAULT;
}
//-----------------------------------------------------------------------//
// app_clear_draw_cb
// - Clear images in principal.
//-----------------------------------------------------------------------//
int app_clear_draw_cb(Ihandle * self) 
{
	// Update image in canvas only a time for one loop in idle
	// Get focus of principal canvas 
	IupGLMakeCurrent(self);
	glClearColor(1.0,1.0,1.0,1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	return IUP_DEFAULT;
}

//--------------------------------------------------------------------------------//
// app_draw_video_image_cb
// - Draw images in the canvas.
//--------------------------------------------------------------------------------//
void app_draw_video_image_cb(Ihandle * self,Scene &scene ) 
{
	if(scene._imageDataSource == NULL)
		return;
	int w = scene._imageDataSource->getWidth();
	int h = scene._imageDataSource->getHeight();

	// Get focus of principal canvas 
	IupGLMakeCurrent(self);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glViewport(0,0,w,h);
	glRasterPos2f(0.f,0.f);
	
	glDrawPixels(w, h, GL_RGB, GL_UNSIGNED_BYTE, scene._imageCurrentRGB->imageData);	
}

//-----------------------------------------------------------------------------//
// app_draw_track_points_cb
// - Draw 2D coordinates of markers
//-----------------------------------------------------------------------------//
void renderBitmapString(
						float x, 
						float y, 
						float z, 
						void *font, 
						char *string) {  
							char *c;
							glRasterPos3f(x, y,z);
							for (c=string; *c != '\0'; c++) {
								glutBitmapCharacter(font, *c);
							}
}


int init_video_canvas(Ihandle * self, int w, int h) 
{
	// Get focus of principal canvas 
	IupGLMakeCurrent(self);
	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();
	gluOrtho2D (0.0, (GLdouble)w, 0.0, (GLdouble)h);
	glMatrixMode(GL_MODELVIEW);	
	glLoadIdentity();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor(0.5,0.5,0.5,1.0);	
	
	return IUP_DEFAULT;
}

void app_draw_teapot_cb( Ihandle * self ,Processing * proc )
{
	if (proc == NULL)
		return;

	double size = 0.08;

	IupGLMakeCurrent(self);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadMatrixd(proc->getGLProjectionMatrix());

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadMatrixd(proc->getGLModelViewMatrix());

	

	glLineWidth(2);
	
	glBegin(GL_LINES);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.0f,0.0f,0.0f);
	glVertex3f(0.0f,size,0.0f);

	glColor3f(0.0f, 0.8f, 0.8f);
	glVertex3f(0.0f,0.0f,0.0f);
	glVertex3f(size,0.0f,0.0f);

	glColor3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0.0f,0.0f,0.0f);
	glVertex3f(0.0f,0.0f,size);
	glEnd();


	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	
	return ;

}